import type { CesiumCommand } from "../command/Command";
import OpenAI from "openai";
/**
 * 基于Deepseek的自然语言解析器，生成Cesium的指令
 */
class DeepseekNLP {
	private _baseURL: string = "http://127.0.0.1:5173/v1";
	private _apiKey: string = "ollama";
	private _model: string = "deepseek-r1:14b";

	constructor() {}

	public async parseNLCommand(text: string): Promise<CesiumCommand> {
		const openai = new OpenAI({
			baseURL: this._baseURL,
			apiKey: this._apiKey,
			dangerouslyAllowBrowser: true,
		});

		// const openai = new OpenAI({
		// 	baseURL: "https://api.deepseek.com",
		// 	apiKey: DEEPSEEK_API_KEY,
		// 	dangerouslyAllowBrowser: true,
		// });

		let system_prompt = this._getSystemPrompt();
		const response = await openai.chat.completions.create({
			//model: "deepseek-chat",
			model: this._model,
			messages: [
				{
					role: "system",
					content: system_prompt,
				},
				{
					role: "user",
					content: text,
				},
			],
			response_format: { type: "json_object" }, // 要求返回JSON
		});

		let content = response.choices[0].message.content;
		console.log(content);
		return this._parseResponse(content);
	}

	private _parseResponse(response: string): CesiumCommand {
		return JSON.parse(response);
	}

	private _getSystemPrompt(): string {
		return `你是Cesium专家，将用户指令转换为JSON格式:
          {
            "action": "flyTo|load3DTiles|measure|setView|setOrientation",
            "params": {
              "position" : {
                "longitude": 0, //number
                "latitude": 0,  //number
                "height": 0,    //number
              },
              "orientation":{
                "heading: 0     //number                
                "pitch": 0      //number
                "roll" : 0,     //number
              },
              "url": "3DTiles服务地址",
              "duration": 飞行时间
            }
          }`;
	}
}

export default DeepseekNLP;
